#pragma once

#include "channel.h"

class poll_poller;

class connection final : public channel
{
  public:
    connection(poll_poller* poller, struct sockaddr* server_addr_ptr);
    ~connection();

    void connect_server();

    void handle_read();
    void handle_write();
    void handle_close();
    void handle_error();

  private:
    enum CONN_STATE
    {
        DISCONNECTED,
        CONNECTING,
        CONNECTED
    };
    void        retry_connect();
    void        connecting();
    std::string get_addr() const;

  private:
    CONN_STATE           state_           = DISCONNECTED;
    const int            retry_interval_  = 3000;
    struct sockaddr*     server_addr_ptr_ = nullptr;
    static constexpr int ONCE_READ_LENGTH = 1024;
    char                 readbuffer_[ONCE_READ_LENGTH + 1];
};